package com.lyl.novel.common.config;

import com.lyl.novel.common.utils.QuartzUtils;
import com.lyl.novel.model.entity.Task;
import com.lyl.novel.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Trigger;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Mr.Luo
 * @date 2022-08-23 14:07
 * @desc
 */
@Slf4j
@Component
public class QuartzConfig implements CommandLineRunner {
    @Resource
    private TaskService taskService;

    @Override
    public void run(String... args) throws Exception {
        log.info("开始初始化定时任务");
        List<Task> list = taskService.list();
        list.stream()
                .filter(task -> Trigger.TriggerState.NORMAL.name().equals(task.getJobStatus()))
                .forEach(QuartzUtils::addJob);
        log.info("定时任务初始化完成");
    }
}
